package com.jshoperxms.components.neteaseim;

import com.jshoperxms.Response;
import com.jshoperxms.controller.mall.backstage.base.BaseTController;
import com.jshoperxms.controller.utils.SHA1;
import com.jshoperxms.controller.utils.statickey.SerialKeys;
import com.jshoperxms.controller.utils.statickey.TagWords;
import com.jshoperxms.controller.utils.statickey.WarnWrods;
import com.jshoperxms.entity.MemberT;
import com.jshoperxms.service.BasicUserTService;
import com.jshoperxms.service.MemberTService;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;

/**
 * Created by sdywcd on 17/1/4.
 * Des:网易云信IM基础创建服务
 */
@Controller
@RequestMapping("/mall/neteaseim")
public class NeteaseImController extends BaseTController {


    @Resource
    private BasicUserTService basicUserTService;

    @Resource
    private MemberTService memberTService;
    private Map<String,Object> responsedata=new HashMap<>();

    /**
     * 获取后台用户的云信id
     * @return
     * @throws IOException
     */
    @RequestMapping(value="/createBasicUserNimId",method= RequestMethod.POST)
    public ResponseEntity createBasicUserNimId(@RequestBody NimVo rq) throws IOException {
        DefaultHttpClient httpClient=new DefaultHttpClient();
        HttpPost httpPost=new HttpPost(NeteaseUrl.CREATEIM);
        String nonce=String.valueOf((new Date().getTime()/1000L));
        String curtime=nonce;
        String checksum=CheckSumBuilder.getCheckSum(Config.appSecret,nonce,curtime);
        httpPost.addHeader(ParamsKey.APPKEY,Config.appKey);
        httpPost.addHeader(ParamsKey.NONCE,nonce);
        httpPost.addHeader(ParamsKey.CURTIME,curtime);
        httpPost.addHeader(ParamsKey.CHECKSUM,checksum);
        httpPost.addHeader(ParamsKey.CONTENTTYPE, ParamsKey.CONTENTVALUE);
        String token=SHA1.getDigestOfString(rq.getBasicuserid()+ SerialKeys.BasicUserT);
        // 设置请求的参数
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("accid", rq.getBasicuserid()));
        nvps.add(new BasicNameValuePair("name",rq.getName()));
        nvps.add(new BasicNameValuePair("icon",rq.getIcon()));
        nvps.add(new BasicNameValuePair("token", token));
        httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
        // 执行请求
        HttpResponse response = httpClient.execute(httpPost);
        String data=EntityUtils.toString(response.getEntity(),"utf-8");
        responsedata.put(TagWords.BEAN, data);
        responsedata.put(TagWords.TOKEN,token);
        responsedata.put("appkey",Config.appKey);
        responsedata.put("accid",rq.getBasicuserid());
        responsedata.put(TagWords.SUCCESS, true);
        responsedata.put(TagWords.MSG,TagWords.OPERATION_SUCCESS);
        return Response.success(responsedata);
    }


    /**
     * 获取会员的云信Id
     * @return
     * @throws IOException
     */
    @RequestMapping(value="/createMemberNimId",method= RequestMethod.POST)
    public ResponseEntity createMemberNimId(@RequestBody NimVo rq) throws IOException {
        MemberT bean=this.memberTService.findByPK(MemberT.class,rq.getMemberid());
        if(bean!=null){
            DefaultHttpClient httpClient=new DefaultHttpClient();
            HttpPost httpPost=new HttpPost(NeteaseUrl.CREATEIM);
            String nonce=String.valueOf((new Date().getTime()/1000L));
            String curtime=nonce;
            String checksum=CheckSumBuilder.getCheckSum(Config.appSecret,nonce,curtime);
            httpPost.addHeader("AppKey",Config.appKey);
            httpPost.addHeader("Nonce",nonce);
            httpPost.addHeader("CurTime",curtime);
            httpPost.addHeader("CheckSum",checksum);
            httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
            String token=SHA1.getDigestOfString(bean.getId()+SerialKeys.MEMBER);
            // 设置请求的参数
            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
            nvps.add(new BasicNameValuePair("accid", bean.getId()));
            nvps.add(new BasicNameValuePair("name",bean.getNick()));
            nvps.add(new BasicNameValuePair("icon",bean.getHeadpath()));
            nvps.add(new BasicNameValuePair("token",token));
            httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
            // 执行请求
            HttpResponse response = httpClient.execute(httpPost);
            String data=EntityUtils.toString(response.getEntity(),"utf-8");
            responsedata.put(TagWords.BEAN, data);
            responsedata.put(TagWords.TOKEN,token);
            responsedata.put("appkey",Config.appKey);
            responsedata.put(TagWords.SUCCESS, true);
            responsedata.put(TagWords.MSG,TagWords.OPERATION_SUCCESS);
        }else{
            responsedata.put(TagWords.SUCCESS, false);
            responsedata.put(TagWords.MSG, WarnWrods.EMPTY_ACCOUNT);
        }
        return Response.success(responsedata);
    }



    /**
     * 更新后台用户云信ID
     * @return
     * @throws IOException
     */
    @RequestMapping(value="/updateBasicUserNimId",method= RequestMethod.POST)
    public ResponseEntity updateBasicUserNimId(@RequestBody NimVo rq) throws IOException {

        DefaultHttpClient httpClient=new DefaultHttpClient();
        HttpPost httpPost=new HttpPost(NeteaseUrl.UPDATEIM);
        String nonce=String.valueOf((new Date().getTime()/1000L));
        String curtime=nonce;
        String checksum=CheckSumBuilder.getCheckSum(Config.appSecret,nonce,curtime);
        httpPost.addHeader("AppKey",Config.appKey);
        httpPost.addHeader("Nonce",nonce);
        httpPost.addHeader("CurTime",curtime);
        httpPost.addHeader("CheckSum",checksum);
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        String token=SHA1.getDigestOfString(rq.getAccid()+ SerialKeys.BasicUserT);
        // 设置请求的参数
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("accid", rq.getAccid()));
        nvps.add(new BasicNameValuePair("token",token));
        httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
        // 执行请求
        HttpResponse response = httpClient.execute(httpPost);
        String data=EntityUtils.toString(response.getEntity(),"utf-8");
        responsedata.put(TagWords.BEAN, data);
        responsedata.put(TagWords.TOKEN,token);
        responsedata.put(TagWords.SUCCESS, true);
        responsedata.put(TagWords.MSG,TagWords.OPERATION_SUCCESS);

        return Response.success(responsedata);
    }





    /**
     * 更新会员云信ID
     * @return
     * @param  rq accid 就是memberid
     * @throws IOException
     */
    @RequestMapping(value="/updateMemberNimId",method= RequestMethod.POST)
    public ResponseEntity updateMemberNimId(@RequestBody NimVo rq) throws IOException {

        DefaultHttpClient httpClient=new DefaultHttpClient();
        HttpPost httpPost=new HttpPost(NeteaseUrl.UPDATEIM);
        String nonce=String.valueOf((new Date().getTime()/1000L));
        String curtime=nonce;
        String checksum=CheckSumBuilder.getCheckSum(Config.appSecret,nonce,curtime);
        httpPost.addHeader("AppKey",Config.appKey);
        httpPost.addHeader("Nonce",nonce);
        httpPost.addHeader("CurTime",curtime);
        httpPost.addHeader("CheckSum",checksum);
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        String token= SHA1.getDigestOfString(rq.getAccid()+SerialKeys.MEMBER);
        // 设置请求的参数
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("accid", rq.getAccid()));
        nvps.add(new BasicNameValuePair("token",token));
        httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
        // 执行请求
        HttpResponse response = httpClient.execute(httpPost);
        String data=EntityUtils.toString(response.getEntity(),"utf-8");
        responsedata.put(TagWords.BEAN, data);
        responsedata.put(TagWords.TOKEN,token);
        responsedata.put(TagWords.SUCCESS, true);
        responsedata.put(TagWords.MSG,TagWords.OPERATION_SUCCESS);
        return Response.success(responsedata);
    }



    /**
     * 获取最新的token
     * @return
     * @param  rq accid 就是memberid
     * @throws IOException
     */
    @RequestMapping(value="/refreshToken",method= RequestMethod.POST)
    public ResponseEntity refreshToken(@RequestBody NimVo rq) throws IOException {

        DefaultHttpClient httpClient=new DefaultHttpClient();
        HttpPost httpPost=new HttpPost(NeteaseUrl.REFRESHTOKEN);
        String nonce=String.valueOf((new Date().getTime()/1000L));
        String curtime=nonce;
        String checksum=CheckSumBuilder.getCheckSum(Config.appSecret,nonce,curtime);
        httpPost.addHeader("AppKey",Config.appKey);
        httpPost.addHeader("Nonce",nonce);
        httpPost.addHeader("CurTime",curtime);
        httpPost.addHeader("CheckSum",checksum);
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        // 设置请求的参数
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("accid", rq.getAccid()));
        httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
        // 执行请求
        HttpResponse response = httpClient.execute(httpPost);
        String data=EntityUtils.toString(response.getEntity(),"utf-8");
        responsedata.put(TagWords.BEAN, data);
        responsedata.put(TagWords.SUCCESS, true);
        responsedata.put(TagWords.MSG,TagWords.OPERATION_SUCCESS);
        return Response.success(responsedata);
    }

    /**
     * 禁止云信ID
     * @return
     * @param  rq accid
     * @throws IOException
     */
    @RequestMapping(value="/blockNim",method= RequestMethod.POST)
    public ResponseEntity blockNim(@RequestBody NimVo rq) throws IOException {

        DefaultHttpClient httpClient=new DefaultHttpClient();
        HttpPost httpPost=new HttpPost(NeteaseUrl.BLOCK);
        String nonce=String.valueOf((new Date().getTime()/1000L));
        String curtime=nonce;
        String checksum=CheckSumBuilder.getCheckSum(Config.appSecret,nonce,curtime);
        httpPost.addHeader("AppKey",Config.appKey);
        httpPost.addHeader("Nonce",nonce);
        httpPost.addHeader("CurTime",curtime);
        httpPost.addHeader("CheckSum",checksum);
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        // 设置请求的参数
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("accid", rq.getAccid()));
        nvps.add(new BasicNameValuePair("needkick","true"));
        httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
        // 执行请求
        HttpResponse response = httpClient.execute(httpPost);
        String data=EntityUtils.toString(response.getEntity(),"utf-8");
        responsedata.put(TagWords.BEAN, data);
        responsedata.put(TagWords.SUCCESS, true);
        responsedata.put(TagWords.MSG,TagWords.OPERATION_SUCCESS);
        return Response.success(responsedata);
    }


    /**
     * 解禁云信ID
     * @return
     * @param  rq accid
     * @throws IOException
     */
    @RequestMapping(value="/unblockNim",method= RequestMethod.POST)
    public ResponseEntity unblockNim(@RequestBody NimVo rq) throws IOException {

        DefaultHttpClient httpClient=new DefaultHttpClient();
        HttpPost httpPost=new HttpPost(NeteaseUrl.BLOCK);
        String nonce=String.valueOf((new Date().getTime()/1000L));
        String curtime=nonce;
        String checksum=CheckSumBuilder.getCheckSum(Config.appSecret,nonce,curtime);
        httpPost.addHeader("AppKey",Config.appKey);
        httpPost.addHeader("Nonce",nonce);
        httpPost.addHeader("CurTime",curtime);
        httpPost.addHeader("CheckSum",checksum);
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        // 设置请求的参数
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("accid", rq.getAccid()));
        httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
        // 执行请求
        HttpResponse response = httpClient.execute(httpPost);
        String data=EntityUtils.toString(response.getEntity(),"utf-8");
        responsedata.put(TagWords.BEAN, data);
        responsedata.put(TagWords.SUCCESS, true);
        responsedata.put(TagWords.MSG,TagWords.OPERATION_SUCCESS);
        return Response.success(responsedata);
    }





}
